import MinRequest from './min-request'
import { isH5 } from '@/utils/platform'
import { minCache } from '@/utils/min-cache'
import { formHasValue } from '@/utils/form-has-value'

const minRequest = new MinRequest()
// 请求拦截器
minRequest.interceptors.request((request) => {
  request.data = request.noFormHasValue ? request.data : formHasValue(request.data, false)
  if (minCache.get('ut')) {
    request.header.Authorization = `Bearer ${minCache.get('ut')}`
  }

  if (request.contentType == 'fd') {
    request.header['Content-Type'] = 'application/x-www-form-urlencoded'
  }

  return request
})

// 响应拦截器
minRequest.interceptors.response((res) => res)

// 设置默认配置
minRequest.setConfig((config) => {
  if (isH5) {
    // 生产环境请求的url，不带api
    if (process.env.NODE_ENV == 'production') {
      config.baseURL = process.env.VUE_APP_API_URL
    } else {
      config.baseURL = '/api'
    }
  } else {
    config.baseURL = process.env.VUE_APP_API_URL
  }
  return config
})

export default minRequest
